import java.util.Date;

public class RopeState {

	boolean master;		// true if representing the master or server
	
	int ropePosition;	// value between -1000 and 1000
	int ropeForce;		// value between -1000 and 1000
	int forceMaster;	// value between 0 and 1000
	int forceSlave;		// value between 0 and 1000

	boolean connected;	// true if there is an open skype connection between master and slave
	int gameState;		// 0 -> countdown, 1 -> playing, 2 -> finished
	long gameStartTime;	// the time when the game started
	long timeOffset;
	LocalData localdata;	// some info for local client (time offset, sensitivity etc)

	public static final int COUNTDOWN = 0;
	public static final int PLAYING = 1;
	public static final int GAMEOVER = 2;	

	
	public RopeState(boolean master, LocalData ld) {
		this.master = master;
		ropePosition = 0;
		ropeForce = 0;
		forceMaster = 0;
		forceSlave = 0;
		connected = false;
		gameState = GAMEOVER;
		localdata = ld;
		//timeOffset = ld.getTimeOffset();		
		gameStartTime = (new Date()).getTime();
		System.out.println("Raw time: " + gameStartTime); 
		gameStartTime = gameStartTime + localdata.getTimeOffset();
		System.out.println("Current time offset: " + localdata.getTimeOffset() + " ms");		
		System.out.println("Corrected time: " + gameStartTime);

	}

	
	public boolean isMaster() {
		return master;
	}

	
	public int getRopePosition() {
		return ropePosition;
	}

	public int getRopeForce() {
		return ropeForce;
	}

	public int getMyForce() {
		if (master) return forceMaster;
		return forceSlave;
	}

	public int getOpponentForce() {
		if (master) return forceSlave;
		return forceMaster;
	}

	public int getMasterForce() {
		return forceMaster;
	}

	public int getSlaveForce() {
		return forceSlave;	
	}
	
	public void setRopePosition(int pos) {
		ropePosition = pos;
	}

	public void setRopeForce(int force) {
		ropeForce = force;
	}

	public void setMyForce(int force) {
		if (master) forceMaster = force;
		else forceSlave = force;
	}

	public void setOpponentForce(int force) {
		if (! master) forceMaster = force;
		else forceSlave = force;
	}

	
	public String toString() {
		return "" + ropePosition + ";" + ropeForce + ";" + forceMaster + ";" + forceSlave + ";" + gameState + ";" + gameStartTime;
	}

	public void parse(String s) {
		String[] t = s.split(";");
		if (t.length == 6) {
			ropePosition = Integer.parseInt(t[0]);
			ropeForce = Integer.parseInt(t[1]);
			forceMaster = Integer.parseInt(t[2]);
			//forceSlave = Integer.parseInt(t[3]);
			gameState = Integer.parseInt(t[4]);
			gameStartTime = Long.parseLong(t[5]);
		}
	}

	
	public void setConnected(boolean connected) {
		this.connected = connected;
	}
	
	public boolean isConnected() {
		return connected;
	}

	public void setGameState(int gameState) {
		if (gameState == COUNTDOWN) {
			ropePosition = 0;
			ropeForce = 0;
			gameStartTime = (new Date()).getTime() + localdata.getTimeOffset();
		}
		this.gameState = gameState;
	}
	
	public int getGameState() {
		return gameState;
	}
	
	public long getElapsedTime() {
		return (( ( (new Date()).getTime() + localdata.getTimeOffset()) - gameStartTime) / 1000) - 10;
	}
	
}
